<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>九宫格求解</title>
</head>
<body>
<h1>九宫格求解</h1>
<script>
    let max_n = 10;
    let answer = [0, 0, 0, 0, 0, 0];

    function isRepeat(arr) {
        for (var i = 0; i < answer.length - 1; i++) {
            for (var j = i + 1; j < answer.length; j++) {
                if (arr[i] == arr[j]) return false;
            }
        }
        return true;
    }

    function verification(answer) {
        let sum = answer[0] + answer[1] + 6;
        return !((sum != 4 + answer[2] + answer[3]) || (sum != 4 + answer[4] + answer[5]) || (sum != 4 + answer[0] + answer[4]) ||
            (sum != answer[1] + answer[2] + answer[5]) || (sum != 16 + answer[3]) || (sum != answer[0] + answer[2] + 10) || (sum != 6 + answer[2] + answer[4]));
    }

    for (; answer[0] < max_n; answer[0]++) {
        for (; answer[1] < max_n; answer[1]++) {
            for (; answer[2] < max_n; answer[2]++) {
                for (; answer[3] < max_n; answer[3]++) {
                    for (; answer[4] < max_n; answer[4]++) {
                        for (; answer[5] < max_n; answer[5]++) {
                            if (!isRepeat(answer)) continue;
                            if (isRepeat(answer) && verification(answer)) {
                                document.write("成立!\n");
                                console.log("成立");
                                document.write(answer[0] + " " + answer[1] + " " + 6 + "\n");
                                console.log(answer[0] + " " + answer[1] + " " + 6 + "\n");
                                document.write(4 + " " + answer[2] + " " + answer[3] + "\n");
                                console.log(4 + " " + answer[2] + " " + answer[3] + "\n");
                                document.write(answer[4] + " " + answer[5] + " " + 10 + "\n");
                                console.log(answer[4] + " " + answer[5] + " " + 10 + "\n\n");
                            } else if (isRepeat(answer)) {
                                document.write("不成立:\n");
                                console.log("不成立");
                                document.write(answer[0] + " " + answer[1] + " " + 6 + "\n");
                                console.log(answer[0] + " " + answer[1] + " " + 6 + "\n");
                                document.write(4 + " " + answer[2] + " " + answer[3] + "\n");
                                console.log(4 + " " + answer[2] + " " + answer[3] + "\n");
                                document.write(answer[4] + " " + answer[5] + " " + 10 + "\n");
                                console.log(answer[4] + " " + answer[5] + " " + 10 + "\n\n");
                            }
                        }
                    }
                }
            }
        }
    }
</script>
</body>
</html>